/*var urlList = new Array();



function pUrl(week,schooltime,venue,url){
    this.week = week;
    this.schooltime  = schooltime;
    this.venue = venue;
    this.url = url;
}*/

$(function(){
    //关闭预加载动画
    okLoading.close($);
    //在获取页面信息是进行ajax访问获取第一页的数据
    $.ajax({
        url: "/getPageByCondition",
        type: "POST",
        success: function (data) {
            //将获取的数组信息打印到body中
            addTableInfo(data);
            console.log(data);//在控制台输出数据
        },
        error: function () {
            alert("查询失败！");
        }
    });
		
	//更新课程信息
	$("#updateCourse").click(function(){
		var cname = $("#courseName").val().replace(/(^\s*)|(\s*$)/g, '');
		var teacher = $("#courseTeacher").val().replace(/(^\s*)|(\s*$)/g, '');
		var price = $("#coursePrice").val();
		if(isNaN(price)){
			return false;
		}else if(cname == '' || cname == undefined || cname == null){
			alert(1);
			return false;
		}else if(teacher == '' || teacher == undefined || teacher == null){
			alert(1);
			return false;
		}
		$.post("/updateCourseInPage",{"cid":$("#courseId").val(),
            "cname":$("#courseName").val(),
            "teacher":$("#courseTeacher").val(),
            "week":$("#courseWeek").val(),
            "schooltime":$("#courseSchooltime").val(),
            "mold":$("#courseMold").val(),
            "venue":$("#courseVenue").val(),
            "price":$("#coursePrice").val(),
            "current":$("#one").text()},
			function (data) {
                if(data.current==0){
                    $("#info").css("display","block");
                    $("#msg").html("<p>失败</p>" +
                        "<p style='font-size: 20px;color: red;'>与已存在课程存在冲突</p>");
                    window.setTimeout(function(){
                        $("#info").css("display","none");
                    },2500);
                    return false;
                }
                addTableInfo(data);
                $("#info").css("display","block");
                $("#msg").html("<p>&nbsp;</p>" +
                    "<p style='font-size: 40px;'>成功</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);

        });


	});
		//代码块，用于实现精确查询的js效果
	   {
	   	$("#qcname").click(function(){
    				var result = $("#qcnameValue").val();
    				if(result==0){
    					$("#qcname").css("background-color","#00C292");
				  		$("#qcnameValue").val(1);
				  		$("#qcnameDiv").slideDown(300);
    				}else if(result==1){
    					$("#qcname").css("background-color","#B8C3C5");
				  		$("#qcnameValue").val(0);
				  		$("#qcnameDiv").slideUp(300);
    				}				
    			});
    			$("#qteacher").click(function(){
    				var result = $("#qteacherValue").val();
    				if(result==0){
    					$("#qteacher").css("background-color","#00C292");
				  		$("#qteacherValue").val(1);
				  		$("#qteacherDiv").slideDown(300);
    				}else if(result==1){
    					$("#qteacher").css("background-color","#B8C3C5");
				  		$("#qteacherValue").val(0);
				  		$("#qteacherDiv").slideUp(300);
    				}				
    			});
    			$("#qvenue").click(function(){
    				var result = $("#qvenueValue").val();
    				if(result==0){
    					$("#qvenue").css("background-color","#00C292");
				  		$("#qvenueValue").val(1);
				  		$("#qvenueDiv").slideDown(300);
    				}else if(result==1){
    					$("#qvenue").css("background-color","#B8C3C5");
				  		$("#qvenueValue").val(0);
				  		$("#qvenueDiv").slideUp(300);
    				}				
    			});
    			$("#qweek").click(function(){
    				var result = $("#qweekValue").val();
    				if(result==0){
    					$("#qweek").css("background-color","#00C292");
				  		$("#qweekValue").val(1);
				  		$("#qweekDiv").slideDown(300);
    				}else if(result==1){
    					$("#qweek").css("background-color","#B8C3C5");
				  		$("#qweekValue").val(0);
				  		$("#qweekDiv").slideUp(300);
    				}				
    			});
    			$("#qtime").click(function(){
    				var result = $("#qtimeValue").val();
    				if(result==0){
    					$("#qtime").css("background-color","#00C292");
				  		$("#qtimeValue").val(1);
				  		$("#qtimeDiv").slideDown(300);
    				}else if(result==1){
    					$("#qtime").css("background-color","#B8C3C5");
				  		$("#qtimeValue").val(0);
				  		$("#qtimeDiv").slideUp(300);
    				}				
    			});
    			$("#qmold").click(function(){
    				var result = $("#qmoldValue").val();
    				if(result==0){
    					$("#qmold").css("background-color","#00C292");
				  		$("#qmoldValue").val(1);
				  		$("#qmoldDiv").slideDown(300);
    				}else if(result==1){
    					$("#qmold").css("background-color","#B8C3C5");
				  		$("#qmoldValue").val(0);
				  		$("#qmoldDiv").slideUp(300);
    				}				
    			});
    			$("#qprice").click(function(){
    				var result = $("#qpriceValue").val();
    				if(result==0){
    					$("#qprice").css("background-color","#00C292");
				  		$("#qpriceValue").val(1);
				  		$("#qpriceDiv").slideDown(300);
    				}else if(result==1){
    					$("#qprice").css("background-color","#B8C3C5");
				  		$("#qpriceValue").val(0);
				  		$("#qpriceDiv").slideUp(300);
    				}				
    			});   	}	
	
	//精确查询课程信息
	$("#bestQuery").click(function(){
		    var queryCourse = new Object();
		 	if($("#qcnameValue").val()==1){
		 		queryCourse.cname = $("#divcname").val();
		 	}
		 	if($("#qmoldValue").val()==1){
		 		queryCourse.mold = $("#divmold").val();
		 	}
		 	if($("#qteacherValue").val()==1){
		 		queryCourse.teacher = $("#divteacher").val();
		 	}
		 	if($("#qweekValue").val()==1){
		 		queryCourse.week = $("#divweek").val();
		 	}
		 	if($("#qtimeValue").val()==1){
		 		queryCourse.schooltime = $("#divtime").val();
		 	}
		 	if($("#qvenueValue").val()==1){
		 		queryCourse.venue = $("#divvenue").val();
		 	}
		 	if($("#qpriceValue").val()==1){
		 		queryCourse.priceStart = $("#divpriceStart").val();
		 		queryCourse.priceEnd = $("#divpriceEnd").val();
		 	}
		 		
		 	console.log(queryCourse);	
		 	$.ajax({
		             url: "/getPageByQuery",
		             type: "POST",
		             data: queryCourse,                    
		             success: function (data) {
		             	//将获取的数组信息打印到body中
						addTableInfo(data);
						$("#judgeQuery").val(1);
		             },
		             error: function () {
		                 alert("查询失败！");
		
		             }
	         });			
		
	});
	
   	//模糊查询
	$("#likeQuery").click(function(){
		console.log($("#likeValue").val()); 
		$.ajax({
		             url: "/getPageByCondition",
		             type: "POST",
		             data: {"condition":$("#likeValue").val()},                    
		             success: function (data) {
		             	//将获取的数组信息打印到body中
						addTableInfo(data);
                         $("#judgeQuery").val(0);
                         console.log(data);
		             },
		             error: function () {
		                 alert("查询失败！");
		             }
	        });
        $("#likeValue").val("");
	});
	
	//课程添加相关js
	$("#addCourse").click(function(){
		var course = {
		"cname":$("#addcourseName").val(),
		"teacher":$("#addcourseTeacher").val(),
		"sample":$("#fileName").val(),
		"week":$("#addcourseWeek").val(),
		"schooltime":$("#addcourseSchooltime").val(),
		"mold":$("#addcourseMold").val(),
		"venue":$("#addcourseVenue").val(),
		"price":$("#addcoursePrice").val()};
		var cname = $("#addcourseName").val().replace(/(^\s*)|(\s*$)/g, '');
		var teacher = $("#addcourseTeacher").val().replace(/(^\s*)|(\s*$)/g, '');
		var price = $("#addcoursePrice").val().replace(/(^\s*)|(\s*$)/g, '');
		if(isNaN(price)||price==null){
            $("#info").css("display","block");
            $("#msg").html("<p>&nbsp;</p>" +
                "<p style='font-size: 20px;color: red;'>价格只能是数字</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },2500);
			return false;
		}else if(cname == '' || cname == undefined || cname == null){
            $("#info").css("display","block");
            $("#msg").html("<p>&nbsp;</p>" +
                "<p style='font-size: 20px;color: red;'>课程名称不能为空</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },2500);
			return false;
		}else if(teacher == '' || teacher == undefined || teacher == null){
            $("#info").css("display","block");
            $("#msg").html("<p>&nbsp;</p>" +
                "<p style='font-size: 20px;color: red;'>教练名称不能为空</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },2500);
			return false;
		}		
		console.log(course);
		$.post("/addCourse",course,function (data) {
			if(data==1){
                $("#info").css("display","block");
                $("#msg").html("<p>&nbsp;</p>" +
                    "<p style='font-size: 40px;'>成功</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);

                $.ajax({
                    url: "/getPageByCondition",
                    type: "POST",
                    success: function (data) {
                        //将获取的数组信息打印到body中
                        addTableInfo(data);
                        console.log(data);//在控制台输出数据
						$("#fileName").val("feature-thumb1.png");
                    },
                    error: function () {
                        alert("查询失败！");
                    }
                });

			}else{
                $("#info").css("display","block");
                $("#msg").html("<p>失败</p>" +
                    "<p style='font-size: 20px;color: red;'>与已存在课程存在冲突</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },2500);
			}
        });

		
	});
	
	//上传图片
	$("#pic").click(function () {
			$("#upload").click(); //隐藏了input:file样式后，点击头像就可以本地上传
			$("#upload").on("change",function(){
				 var formData = new FormData();
         formData.append('file', $('#upload')[0].files[0]);
         formData.append("selectId", '0');
         console.log(formData);
         $.ajax({
             url: "/courseUpload",
             type: "POST",
             data: formData,
             contentType: false,             
             processData: false,
             success: function (data) {
				$("#fileName").val(data);
             },
             error: function () {
                 alert("上传失败！");

             }
         });							
		var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径，该路径不是图片在本地的路径
		if (objUrl) {
				$("#pic").attr("src", objUrl) ; //将图片路径存入src中，显示出图片
				var pUrl = new pUrl();
				pUrl.url = objUrl;
				urlList.push(pUrl);
		}
		});
		});
});
	//取消图片
    function stopUpload(){ 	
    	$("#pic").attr("src","images/feature-thumb1.png");
    	$("#fileName").val("feature-thumb1.png");
    }
	    //建立一個可存取到該file的url
	function getObjectURL(file) {	
		var url = null ;
		if (window.createObjectURL!=undefined) { // basic
			url = window.createObjectURL(file) ;
		} else if (window.URL!=undefined) { // mozilla(firefox)
			url = window.URL.createObjectURL(file) ;
		} else if (window.webkitURL!=undefined) { // webkit or chrome
			url = window.webkitURL.createObjectURL(file) ;
		}
		return url ;
	}				

//通过课程cid获取该课程的所有信息
function getCourseInfo(cid,current){
    $.post("/getCourseByCid",{"cid":cid},function (data){
		$("#courseId").val(data.cid);
		$("#courseName").val(data.cname);
		$("#courseMold").val(data.mold);
		$("#courseSample").attr("src","images/"+data.sample);
		$("#courseWeek").val(data.week);
		$("#courseSchooltime").val(data.schooltime);
		$("#courseTeacher").val(data.teacher);
		$("#courseVenue").val(data.venue);
		$("#coursePrice").val(data.price);
    });

}

function deleteCourseByCid(cid,current){
	if(confirm("确定删除该课程嘛？")){
        $.post("/deleteCourseInPage",{"cid":cid,"current":current},function (data) {
            $("#info").css("display","block");
            $("#msg").html("<p>&nbsp;</p>" +
                "<p style='font-size: 40px;'>成功</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },1500);
            addTableInfo(data);
        });
    }
}

//数据显示的核心代码
function addTableInfo(data){
			$("#one").text(data.current);//设定当前页码
    		$("#pageEnd").val(data.pages);//设定总页数
		    $("#courseTable tr:not(:eq(0))").remove();//在添加数据前,先删除标题以外的内容
		    $("#pages").text(data.pages);//总页数
		    $("#total").text(data.total);//总数
			for(var i=0;i<data.records.length;i++){	
			    if(data.records[i].week==1){
			    	data.records[i].week="周一";
			    }else if(data.records[i].week==2){
			    	data.records[i].week="周二";
			    }else if(data.records[i].week==3){
			    	data.records[i].week="周三";
			    }else if(data.records[i].week==4){
			    	data.records[i].week="周四";
			    }else if(data.records[i].week==5){
			    	data.records[i].week="周五";
			    }else if(data.records[i].week==6){
			    	data.records[i].week="周六";
			    }else if(data.records[i].week==7){
			    	data.records[i].week="周日";
			    }else if(data.records[i].week==8){
			    	data.records[i].week="每周任意";
			    }
				//将records以js的形式添加到指定table中	,不同类型的课程显示不同的颜色	
				if(data.records[i].mold=="公共课"){
		    		$("#courseTable").append("<tr><td>"
		    				+data.records[i].cname+"</td><td>"
		    				+data.records[i].mold+"</td><td>"
		    				+data.records[i].week+"--"+data.records[i].schooltime +"</td><td>"
		    				+data.records[i].teacher+"</td><td>"
		    				+data.records[i].venue+"</td><td>"
		    				+data.records[i].price+"</td><td>"
		    				+data.records[i].searchCount+"</td><td>"
		    				+data.records[i].orderCount+"</td><td>"
		    		  		+"<button class='btn btn-primary' data-toggle='modal' data-target='#update' onclick='getCourseInfo("+data.records[i].cid+","+data.current+")'>更新</button>"+"</td><td>"
                       		+"<button class='btn btn-default' onclick='deleteCourseByCid("+data.records[i].cid+","+data.current+")'>删除</button>"+
                			"</td></tr>");
				}else if(data.records[i].mold=="特色课"){
		    		$("#courseTable").append("<tr class='danger'><td>"
		    				+data.records[i].cname+"</td><td>"
		    				+data.records[i].mold+"</td><td>"
		    				+data.records[i].week+"--"+data.records[i].schooltime +"</td><td>"
		    				+data.records[i].teacher+"</td><td>"
		    				+data.records[i].venue+"</td><td>"
		    				+data.records[i].price+"</td><td>"
		    				+data.records[i].searchCount+"</td><td>"
		    				+data.records[i].orderCount+"</td><td>"
		    		  		+"<button class='btn btn-primary' data-toggle='modal' data-target='#update' onclick='getCourseInfo("+data.records[i].cid+","+data.current+")'>更新</button>"+"</td><td>"
                       		+"<button class='btn btn-default' onclick='deleteCourseByCid("+data.records[i].cid+","+data.current+")'>删除</button>"+
                			"</td></tr>");
				}else if(data.records[i].mold=="增肌课"){
		    		$("#courseTable").append("<tr class='warning'><td>"
		    				+data.records[i].cname+"</td><td>"
		    				+data.records[i].mold+"</td><td>"
		    				+data.records[i].week+"--"+data.records[i].schooltime +"</td><td>"
		    				+data.records[i].teacher+"</td><td>"
		    				+data.records[i].venue+"</td><td>"
		    				+data.records[i].price+"</td><td>"
		    				+data.records[i].searchCount+"</td><td>"
		    				+data.records[i].orderCount+"</td><td>"
		    		  		+"<button class='btn btn-primary' data-toggle='modal' data-target='#update' onclick='getCourseInfo("+data.records[i].cid+","+data.current+")'>更新</button>"+"</td><td>"
                       		+"<button class='btn btn-default' onclick='deleteCourseByCid("+data.records[i].cid+","+data.current+")'>删除</button>"+
                			"</td></tr>");
				}else if(data.records[i].mold=="减脂课"){
		    		$("#courseTable").append("<tr class='active'><td>"
		    				+data.records[i].cname+"</td><td>"
		    				+data.records[i].mold+"</td><td>"
		    				+data.records[i].week+"--"+data.records[i].schooltime +"</td><td>"
		    				+data.records[i].teacher+"</td><td>"
		    				+data.records[i].venue+"</td><td>"
		    				+data.records[i].price+"</td><td>"
		    				+data.records[i].searchCount+"</td><td>"
		    				+data.records[i].orderCount+"</td><td>"
		    		  		+"<button class='btn btn-primary' data-toggle='modal' data-target='#update' onclick='getCourseInfo("+data.records[i].cid+","+data.current+")'>更新</button>"+"</td><td>"
                       		+"<button class='btn btn-default' onclick='deleteCourseByCid("+data.records[i].cid+","+data.current+")'>删除</button>"+
                			"</td></tr>");
				}else if(data.records[i].mold=="康复课"){
		    		$("#courseTable").append("<tr class='success'><td>"
		    				+data.records[i].cname+"</td><td>"
		    				+data.records[i].mold+"</td><td>"
		    				+data.records[i].week+"--"+data.records[i].schooltime +"</td><td>"
		    				+data.records[i].teacher+"</td><td>"
		    				+data.records[i].venue+"</td><td>"
		    				+data.records[i].price+"</td><td>"
		    				+data.records[i].searchCount+"</td><td>"
		    				+data.records[i].orderCount+"</td><td>"
		    		  		+"<button class='btn btn-primary' data-toggle='modal' data-target='#update' onclick='getCourseInfo("+data.records[i].cid+","+data.current+")'>更新</button>"+"</td><td>"
                       		+"<button class='btn btn-default' onclick='deleteCourseByCid("+data.records[i].cid+","+data.current+")'>删除</button>"+
                			"</td></tr>");
				}
			}
			if(data.records.length==0){
                $("#info").css("display","block");
                $("#msg").html("<p>&nbsp;</p>" +
                    "<p style='font-size: 20px;color: red;'>未查到满足要求的课程</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },2500);
                return false;
			}
}

 //翻页，并异步获取页面数据，并显示到页面
  function nextPage(a){
             var judge = parseInt($("#judgeQuery").val());
    		//获取当前的页面情况
    		var pageNow = parseInt($("#one").text());//当前页码
    		var pageEnd = $("#pageEnd").val();//总页数 		
    		if((a==2&pageNow==1)||(a==3&pageNow==pageEnd)||(a==1&pageNow==1)||(a==0&pageNow==pageEnd)){
    			//如果当前页码为1则无法再往前翻一页，反之同理。
    			return false; 		
    		}
    		if(a==1){//首页 传入页码current=1

    			if(judge==0){
                $.post("/getPageByCondition",{"current":1},function (data) {
                    addTableInfo(data);
                });
    			}else {
                    $.post("/getOtherPageByQuery",{"current":1},function (data) {
                        addTableInfo(data);
                    });
				}
    		}
    		if(a==2){//上一页 current = current-1;
                if(judge==0){
                    $.post("/getPageByCondition",{"current":pageNow-1},function (data) {
                        addTableInfo(data);
                    });
                }else {
                    $.post("/getOtherPageByQuery",{"current":pageNow-1},function (data) {
                        addTableInfo(data);
                    });
                }
    		}   		
    		if(a==3){//下一页current = current+1;
                if(judge==0){
                    $.post("/getPageByCondition",{"current":pageNow+1},function (data) {
                        addTableInfo(data);
                    });
                }else {
                    $.post("/getOtherPageByQuery",{"current":pageNow+1},function (data) {
                        addTableInfo(data);
                    });
                }
    		}
    		if(a==0){//尾页 current = page;
                if(judge==0){
                    $.post("/getPageByCondition",{"current":pageEnd},function (data) {
                        addTableInfo(data);
                    });
                }else {
                    $.post("/getOtherPageByQuery",{"current":pageEnd},function (data) {
                        addTableInfo(data);
                    });
                }
    		}
    	}
